package com.shop.util;

import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
/**
 * type: 拦截的对象
 * method:拦截的方法
 * args:拦截方法的参数
 * @author Administrator
 */
@Intercepts(value = 
	{ @Signature(args = { MappedStatement.class,Object.class }, 
	                       method = "update", 
	                       type = Executor.class) })
public class MyPlugin implements Interceptor {
	private Properties props;
	/**
	 * 代替拦截对象方法的内容
	 * 如果当前代理的是一个非代理对象，那么就回调真实拦截对象的方法，
	 * 如果不是它会调度下一个插件代理对象的invoke方法
	 * @param arg0
	 * @return
	 * @throws Throwable
	 */
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		System.err.println("intercept===before..........");
		Object obj = invocation.proceed();
		String name = invocation.getMethod().getName();
		System.err.println("intercept===after.............."+name);
		return obj;
	}
	/**
	 * 生成代理对象，使用MyBatis提供的Plugin类的wrap方法
	 * @param target
	 * @return
	 */
	@Override
	public Object plugin(Object target) {
		System.err.println("plugin===调用代理对象。。。" +target.getClass().getName());
		return Plugin.wrap(target, this);
	}

	/**
	 * 获取插件配置的属性,在MyBatis的配置文件里配置
	 */
	@Override
	public void setProperties(Properties props) {
		this.props = props;
		System.err.println(props.get("dbType"));
	}

}
